package org.dromara.hm.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.hm.domain.vo.saleOrder.HmSaleOrderGiftVo;
import org.dromara.hm.domain.bo.HmSaleOrderGiftBo;
import org.dromara.hm.service.IHmSaleOrderGiftService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 订单赠品明细
 *
 * @author Hzy
 * @date 2025-03-17
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/saleOrderGift")
public class HmSaleOrderGiftController extends BaseController {

    private final IHmSaleOrderGiftService hmSaleOrderGiftService;

    /**
     * 查询订单赠品明细列表
     */
    @SaCheckPermission("system:saleOrderGift:list")
    @GetMapping("/list")
    public TableDataInfo<HmSaleOrderGiftVo> list(HmSaleOrderGiftBo bo, PageQuery pageQuery) {
        return hmSaleOrderGiftService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出订单赠品明细列表
     */
    @SaCheckPermission("system:saleOrderGift:export")
    @Log(title = "订单赠品明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HmSaleOrderGiftBo bo, HttpServletResponse response) {
        List<HmSaleOrderGiftVo> list = hmSaleOrderGiftService.queryList(bo);
        ExcelUtil.exportExcel(list, "订单赠品明细", HmSaleOrderGiftVo.class, response);
    }

    /**
     * 获取订单赠品明细详细信息
     *
     * @param giftId 主键
     */
    @SaCheckPermission("system:saleOrderGift:query")
    @GetMapping("/{giftId}")
    public R<HmSaleOrderGiftVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long giftId) {
        return R.ok(hmSaleOrderGiftService.queryById(giftId));
    }

    /**
     * 新增订单赠品明细
     */
    @SaCheckPermission("system:saleOrderGift:add")
    @Log(title = "订单赠品明细", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody HmSaleOrderGiftBo bo) {
        return toAjax(hmSaleOrderGiftService.insertByBo(bo));
    }

    /**
     * 修改订单赠品明细
     */
    @SaCheckPermission("system:saleOrderGift:edit")
    @Log(title = "订单赠品明细", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody HmSaleOrderGiftBo bo) {
        return toAjax(hmSaleOrderGiftService.updateByBo(bo));
    }

    /**
     * 删除订单赠品明细
     *
     * @param giftIds 主键串
     */
    @SaCheckPermission("system:saleOrderGift:remove")
    @Log(title = "订单赠品明细", businessType = BusinessType.DELETE)
    @DeleteMapping("/{giftIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] giftIds) {
        return toAjax(hmSaleOrderGiftService.deleteWithValidByIds(List.of(giftIds), true));
    }
}
